************** Spatial Regressions ******************
* spatial transformation completed previously in spgen_4_* 

************************************
* Contents: country-level regressions with interaction on spatial. 
		* Radiant based: section 2 horizontal, 6 vertical, counter-clockwise.
		* 4950 cells based on k_bins of width 3 KwH 
		* Outcomes: log+1, and ihs transformation
		* standard errors clustered by cell   
************************************

************************************
* Variables
		* cm: country-month items (second level identifier)
		* cell: 1-50 are bunchers at window = 2. (primary identifier)
		* a_bin/e_bin: individual attributes, unstandardized 
		* csd  / ccd: sales / product count in cell. 
		
capture log close 
clear all
set more off 
*set maxvar 30000, permanently // required to load matrix 
* ssc install reghdfe 
* ssc install ftools

* Set directory
global server "R:\WSV2\TBu_AKe\Spatial_NEW" 
cd "$server" // set working directory 

capture mkdir Wmat_SCB_Country
global store "${server}\Wmat_SCB_Country"

*Define sub directories
global desktop "C:\Users\hy65byfe\Desktop\smerge_0712"
*cd "$desktop" // set working directory 


cd "$store"
 log using spatial_6_regs_s_2103_countries, replace   
 

			***************************
			* run regression: OLS, fe *
			***************************	
			
cd "C:\Users\hy65byfe\Desktop\smerge_0712\SCB_4950"	
*cd "R:\WSV2\TBu_AKe\Spatial_NEW\SCB_4950"	
use spatial_regs_s_0611_monthly, replace

*cd "C:\Users\hy65byfe\Desktop\smerge_0712\SCB_4950"			
*use spatial_regs_s_monthly_0102_overlap, replace  
			
*preserve
*spmatrix dir 
keep if year == 2014 // no changes. otherwise matrices are wrong in previous. 	
	
	
			***************************
			***** Single Matrix *****
			***************************	
		

** log and ihs transformations to the untransformed data **

		*** outcomes and polar cases (no q indexing needed, only double loop)
foreach varx of varlist csd L3_sd  		ccd L3_cd     {

   gen     log_`varx'=log(1+`varx')
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5
   quietly replace ihs_`varx'=log(ihs_`varx')
 }
 
 
		*** spatial variables *** 
forvalues q = 1(1)8 { 
	foreach varx of varlist 	S`q'_L3_sd C`q'_L3_sd B`q'_L3_sd S`q'_L3_cd C`q'_L3_cd B`q'_L3_cd	{

   gen     log_`varx'=log(1+`varx')
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5
   quietly replace ihs_`varx'=log(ihs_`varx')
	}
}

			************************************************
			***** Regression with country interaction  *****
			************************************************
			
replace ccode = 7 if ccode == 8 // make list run 1-7 (Serbia was 7, SVN was 8)
		
		******************
		*** Fan (S) ******
		******************

    ** Results for log transformed data **
		 
		forvalues q = 1(1)8 { // loop over directions 
		
			reghdfe  log_csd    log_L3_sd 	log_S`q'_L3_sd		ib3.ccode#c.log_S`q'_L3_sd				if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on wy
			estimates store log_S`q'_1
			
			testparm i.ccode#c.log_S`q'_L3_sd // joint test that all interactions = 0 
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			reghdfe  log_csd    log_L3_sd 	log_S`q'_L3_sd		ib3.ccode#c.log_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on y
			estimates store log_S`q'_2
			
			testparm i.ccode#c.log_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			reghdfe  log_csd    log_L3_sd 	log_S`q'_L3_sd		ib3.ccode#c.log_S`q'_L3_sd	ib3.ccode#c.log_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on both
			estimates store log_S`q'_3
			
			testparm i.ccode#c.log_S`q'_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
		}



		** Results for ihs transformed data **

		forvalues q = 1(1)8 { // loop over directions 
		
			reghdfe  ihs_csd    ihs_L3_sd 	ihs_S`q'_L3_sd		ib3.ccode#c.ihs_S`q'_L3_sd				if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  excl. country i
			estimates store ihs_S`q'_1
			
			testparm i.ccode#c.ihs_S`q'_L3_sd // joint test that all interactions = 0 
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			reghdfe  ihs_csd    ihs_L3_sd 	ihs_S`q'_L3_sd		ib3.ccode#c.ihs_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on y
			estimates store ihs_S`q'_2
			
			testparm i.ccode#c.ihs_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			
			reghdfe  ihs_csd    ihs_L3_sd 	ihs_S`q'_L3_sd		ib3.ccode#c.ihs_S`q'_L3_sd	ib3.ccode#c.ihs_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on both
			estimates store ihs_S`q'_3
			
			testparm i.ccode#c.ihs_S`q'_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
		}		
		
estimates drop *8*		

		******* results log ******
estimates table  log_S*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-wy 				
estimates table  log_S*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-y
estimates table  log_S*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction on both x-var 


		******* results ihs  ******
estimates table  ihs_S*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction country-wy 
estimates table  ihs_S*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)    // ihs, interaction on country-y 				
estimates table  ihs_S*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction on both x-var 

		******* results log ******
estimates table  log_S*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) 				// log, interaction country-wy 				
estimates table  log_S*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01)      			// log, interaction country-y
estimates table  log_S*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) 				// log, interaction on both x-var 


		******* results ihs  ******
estimates table  ihs_S*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01)     			// ihs, interaction country-wy 
estimates table  ihs_S*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) 			    // ihs, interaction on country-y 				
estimates table  ihs_S*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01)				// ihs, interaction on both x-var 
		


*estimates drop _all 
	

		************************
		*** 4 corners (C) ******
		************************
		
*capture noisily { // some specs did not work because the matrix had too many islands and the coefficients were unidentified 
		
    ** Results for log transformed data **
		 
		forvalues q = 1(1)7 { // loop over directions 
		
			reghdfe  log_csd    log_L3_sd 	log_C`q'_L3_sd		ib3.ccode#c.log_C`q'_L3_sd				if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on wy
			estimates store log_C`q'_1
			
			testparm i.ccode#c.log_C`q'_L3_sd // joint test that all interactions = 0 
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			reghdfe  log_csd    log_L3_sd 	log_C`q'_L3_sd		ib3.ccode#c.log_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on y
			estimates store log_C`q'_2
			
			testparm i.ccode#c.log_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			
			reghdfe  log_csd    log_L3_sd 	log_C`q'_L3_sd		ib3.ccode#c.log_C`q'_L3_sd	ib3.ccode#c.log_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on both
			estimates store log_C`q'_3
			
			testparm i.ccode#c.log_C`q'_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
		}



		** Results for ihs transformed data **

		forvalues q = 1(1)7 { // loop over directions 
		
			reghdfe  ihs_csd    ihs_L3_sd 	ihs_C`q'_L3_sd		ib3.ccode#c.ihs_C`q'_L3_sd				if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  excl. country i
			estimates store ihs_C`q'_1
			
			testparm i.ccode#c.ihs_C`q'_L3_sd // joint test that all interactions = 0 
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			reghdfe  ihs_csd    ihs_L3_sd 	ihs_C`q'_L3_sd		ib3.ccode#c.ihs_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on y
			estimates store ihs_C`q'_2
			
			testparm i.ccode#c.ihs_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			
			reghdfe  ihs_csd    ihs_L3_sd 	ihs_C`q'_L3_sd		ib3.ccode#c.ihs_C`q'_L3_sd	ib3.ccode#c.ihs_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on both
			estimates store ihs_C`q'_3
			
			testparm i.ccode#c.ihs_C`q'_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
		}	
		
*}
		
*estimates drop *8*		

		******* results log ******
estimates table  log_C*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-wy 				
estimates table  log_C*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-y
estimates table  log_C*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction on both x-var 


		******* results ihs  ******
estimates table  ihs_C*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction country-wy 
estimates table  ihs_C*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)    // ihs, interaction on country-y 				
estimates table  ihs_C*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction on both x-var 

		******* results log ******
estimates table  log_C*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) 				// log, interaction country-wy 				
estimates table  log_C*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01)      			// log, interaction country-y
estimates table  log_C*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) 				// log, interaction on both x-var 


		******* results ihs  ******
estimates table  ihs_C*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01)     			// ihs, interaction country-wy 
estimates table  ihs_C*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) 			    // ihs, interaction on country-y 				
estimates table  ihs_C*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01)				// ihs, interaction on both x-var 
		
		
*estimates drop _all 

		************************
		*** Beam (B) ***********
		************************
		
    ** Results for log transformed data **
		 
		forvalues q = 1(1)8 { // loop over directions 
		
			reghdfe  log_csd    log_L3_sd 	log_B`q'_L3_sd		ib3.ccode#c.log_B`q'_L3_sd				if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on wy
			estimates store log_B`q'_1
			
			testparm i.ccode#c.log_B`q'_L3_sd // joint test that all interactions = 0 
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			reghdfe  log_csd    log_L3_sd 	log_B`q'_L3_sd		ib3.ccode#c.log_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on y
			estimates store log_B`q'_2
			
			testparm i.ccode#c.log_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			
			reghdfe  log_csd    log_L3_sd 	log_B`q'_L3_sd		ib3.ccode#c.log_B`q'_L3_sd	ib3.ccode#c.log_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on both
			estimates store log_B`q'_3
			
			testparm i.ccode#c.log_B`q'_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
		}



		** Results for ihs transformed data **

		forvalues q = 1(1)8 { // loop over directions 
		
			reghdfe  ihs_csd    ihs_L3_sd 	ihs_B`q'_L3_sd		ib3.ccode#c.ihs_B`q'_L3_sd				if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  excl. country i
			estimates store ihs_B`q'_1
			
			testparm i.ccode#c.ihs_B`q'_L3_sd // joint test that all interactions = 0 
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			reghdfe  ihs_csd    ihs_L3_sd 	ihs_B`q'_L3_sd		ib3.ccode#c.ihs_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on y
			estimates store ihs_B`q'_2
			
			testparm i.ccode#c.ihs_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
			
			reghdfe  ihs_csd    ihs_L3_sd 	ihs_B`q'_L3_sd		ib3.ccode#c.ihs_B`q'_L3_sd	ib3.ccode#c.ihs_L3_sd			if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) //  interaction on both
			estimates store ihs_B`q'_3
			
			testparm i.ccode#c.ihs_B`q'_L3_sd
			estadd scalar F_diff = r(F)
			estadd scalar p_diff = r(p)
			
		}		
		
estimates drop *8*		

		******* results log ******
estimates table  log_B*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-wy 				
estimates table  log_B*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-y
estimates table  log_B*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction on both x-var 


		******* results ihs  ******
estimates table  ihs_B*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction country-wy 
estimates table  ihs_B*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)    // ihs, interaction on country-y 				
estimates table  ihs_B*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction on both x-var 

		******* results log ******
estimates table  log_B*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) 				// log, interaction country-wy 				
estimates table  log_B*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01)      			// log, interaction country-y
estimates table  log_B*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) 				// log, interaction on both x-var 


		******* results ihs  ******
estimates table  ihs_B*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01)     			// ihs, interaction country-wy 
estimates table  ihs_B*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) 			    // ihs, interaction on country-y 				
estimates table  ihs_B*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01)				// ihs, interaction on both x-var 

	
**********************************************************************************************************************************
		
		********************************************
		**** Quick summary, just base + testparm ***
		********************************************
		
		
		******* S: results log ******
estimates table  log_S*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-wy 				
estimates table  log_S*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-y
estimates table  log_S*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction on both x-var 


		******* S: results ihs  ******
estimates table  ihs_S*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction country-wy 
estimates table  ihs_S*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)    // ihs, interaction on country-y 				
estimates table  ihs_S*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction on both x-var 


		******* B: results log ******
estimates table  log_B*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-wy 				
estimates table  log_B*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-y
estimates table  log_B*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction on both x-var 


		******* B: results ihs  ******
estimates table  ihs_B*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction country-wy 
estimates table  ihs_B*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)    // ihs, interaction on country-y 				
estimates table  ihs_B*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction on both x-var 


		******* C: results log ******
estimates table  log_C*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-wy 				
estimates table  log_C*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction country-y
estimates table  log_C*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(log_*)	// log, interaction on both x-var 


		******* C: results ihs  ******
estimates table  ihs_C*_1, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction country-wy 
estimates table  ihs_C*_2, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)    // ihs, interaction on country-y 				
estimates table  ihs_C*_3, b(%9.4f) stats(N r2 F_diff p_diff) star(0.10 .05 .01) keep(ihs_*)	// ihs, interaction on both x-var 



log close 
clear 
			